package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysDictData;
import cn.elead.chaos.remote.web.remote.service.ISysDictDataService;

/**
 * 数据字典信息
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/dict/data")
public class RemoteDictDataController extends BaseController {
	@Autowired
	private ISysDictDataService dictDataService;

	@GetMapping("/page")
	public TableDataInfo<SysDictData> page(SysDictData dictData) {
		startPage();
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		return getDataTable(list);
	}

	@GetMapping("/export")
	@Log(title = "字典数据", businessType = BusinessType.EXPORT)
	public String export(SysDictData dictData) {
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
		return util.exportExcel(list, "字典数据");
	}

	/**
	 * 查询字典数据详细
	 */
	@GetMapping(value = "/{dictCode}")
	public SysDictData getInfo(@PathVariable Long dictCode) {
		return dictDataService.selectDictDataById(dictCode);
	}

	/**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/dictType/{dictType}")
	public List<SysDictData> dictType(@PathVariable String dictType) {
		return dictDataService.selectDictDataByType(dictType);
	}

	/**
	 * 新增字典类型
	 */
	@PostMapping
	@Log(title = "字典数据", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysDictData dict) {
		dict.setCreateBy(SsoUtils.getUserName());
		int insertDictData = dictDataService.insertDictData(dict);
		return insertDictData;
	}

	/**
	 * 修改保存字典类型
	 */
	@PutMapping
	@Log(title = "字典数据", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysDictData dict) {
		dict.setUpdateBy(SsoUtils.getUserName());
		return dictDataService.updateDictData(dict);
	}

	/**
	 * 删除字典类型
	 */
	@DeleteMapping("/{dictCodes}")
	@Log(title = "字典类型", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long[] dictCodes) {
		return dictDataService.deleteDictDataByIds(dictCodes);
	}
}
